/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Materials;

import Raytracing.Shaders.Shader;
import Raytracing.BRDF.LambertBRDF;
import Raytracing.Shaders.SolidTexture;
import SGGUI.GUIComponent;
import SGGUI.GUIEvent;
import SGGUI.GUIListener;
import SGGUI.SGSlider;
import TweetRay.Exportable;
import Materials.MaterialFactory.Materials;
import TweetRay.UTFReader;
import TweetRay.UTFWriter;
import processing.core.PVector;

/**
 *
 * @author Matt
 */
public class SingleColorShader extends Shader implements GUIListener {

    protected PVector color;

    public void setColor(PVector color) {
        this.color = color;
        setFillColor(color);
    }
    public SingleColorShader(String name) {
        super(name);
  
      
        
    }
    
    
    public SingleColorShader(String name, UTFReader reader) {
        super(name);
        
        int rc = reader.getBits(4);
        int gc = reader.getBits(4);
        int bc = reader.getBits(4);

        setColor(new PVector(rc / 15.f, gc / 15.f, bc / 15.f));     
        
        
    }

    public void init() {
        GUIComponent[] controls = new GUIComponent[3];
        controls[0] = new SGSlider("Red", color.x, 1 / 16f, 0, 1.f);
        controls[0].addGUIListener(this);
        controls[1] = new SGSlider("Green", color.y, 1 / 16f, 0, 1.f);
        controls[1].addGUIListener(this);
        controls[2] = new SGSlider("Blue", color.z, 1 / 16f, 0, 1.f);
        controls[2].addGUIListener(this);
        setControls(controls);

    }

    public void GUIEventPerformed(GUIEvent e) {
        if (e.title.equals("Red")) {
            fillColor.x = e.val;
            color.x = e.val;
        }
        if (e.title.equals("Green")) {
            fillColor.y = e.val;
            color.y = e.val;
        }
        if (e.title.equals("Blue")) {
            fillColor.z = e.val;
            color.z = e.val;
        }
        fill = 0xff000000 | ((int) (fillColor.x * 255) << 16)
                | ((int) (fillColor.y * 255) << 8)
                | (int) (fillColor.z * 255);

    }

    public void writeColorBits(UTFWriter output) {

        output.writeBits((int) (fillColor.x * 15), 4);
        output.writeBits((int) (fillColor.y * 15), 4);
        output.writeBits((int) (fillColor.z * 15), 4);

    }
}
